/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bm.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

import bm.data.User;
import bm.forms.UserForm;

/**
 *
 * @author chaosmeleon
 */
public class AddUser extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        System.out.println("adduser action file");
        UserForm userForm = (UserForm) form;
        User user = userForm.getUser();

        this.almacenarUser(user);

        request.getSession().setAttribute("login", user.getLogin());
        request.getSession().setAttribute("name", user.getName());
        request.getSession().setAttribute("apellido", user.getApellido());
        request.setAttribute("user", user);

        user.reset();
        
        return mapping.findForward("verUser");
    }
    
    
    
    private void almacenarUser(User user){
        // Persistencia de datos
        ConnectBD.connectionBD();
        //"login", clave, email, nif, nombre, apellido, direccion, cp, ciudad, provincia, telefono, tipo)
        System.out.println("");
        ConnectBD.insertItemBD("INSERT INTO usuario values ('" + user.getLogin() + "','" + user.getPass() + "','" + user.getMail() + "','" +
                                user.getNif() + "','" + user.getName() + "','" + user.getApellido() + "','" + user.getDireccion() + "','" + user.getCp() +
                                "','" + user.getCiudad() + "','" + user.getProvincia() + "','" + user.getTelefono() + "','" + user.getStatus() + "')");
        ConnectBD.disconnectBD();

    }

}
